package top.myhdg.bijou.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import top.myhdg.bijou.R;

public class SupportActivity extends BaseActivity {

    //==========需要跳转App的包名==========
    private static final String ALIPAY_PACKAGE_NAME = "com.eg.android.AlipayGphone";//支付宝包名
    private static final String INTENT_URL_FORMAT = "intent://platformapi/startapp?saId=10000007&" +
            "clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2F{urlCode}%3F_s" +
            "%3Dweb-other&_t=1472443966571#Intent;" +
            "scheme=alipayqr;package=com.eg.android.AlipayGphone;end";//支付宝转账
    private static final String WECHAT_PACKAGE_NAME = "com.tencent.mm";//微信包名
//    private static final String QQ_PACKAGE_NAME = "com.tencent.mobileqq";//QQ包名
//    private static final String TIM_PACKAGE_NAME = "com.tencent.tim";//Tim包名
    //==========需要跳转App的包名==========

    //==========支持界面控件==========
    Button buttonQQ;//QQ
    Button buttonWeChat;//微信
    Button buttonWeChatMyHdg;//微信订阅号
    Button buttonQQGroup;//QQ群
    Button buttonAliPay;//支付宝转账
    Button exitSupport;//退出支持作者
    //==========支持界面控件==========

    ClipboardManager clipboardManager;//剪贴板

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_support);

        clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);//获取剪贴板
        assert clipboardManager != null;

        //==========QQ/Tim添加好友===========
        buttonQQ = findViewById(R.id.buttonQQ);
        buttonQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(2);
                SupportActivity.this.finish();
//                if (hasClient(SupportActivity.this, QQ_PACKAGE_NAME)) {
//                    clipboardManager.setPrimaryClip(ClipData.newPlainText("text", "1316740440"));
//                    showTipDialog(0, "QQ账号已复制", 900);
//                    Intent intent = SupportActivity.this.getPackageManager().getLaunchIntentForPackage(QQ_PACKAGE_NAME);
//                    startActivity(intent);
//                } else {
//                    showTipDialog(1, "QQ未安装,尝试跳转Tim", 900);
//                    if (hasClient(SupportActivity.this, TIM_PACKAGE_NAME)) {
//                        Intent intent = SupportActivity.this.getPackageManager().getLaunchIntentForPackage(TIM_PACKAGE_NAME);
//                        startActivity(intent);
//                    } else {
//                        showTipDialog(1, "Tim未安装", 900);
//                    }
//                }
            }
        });
        //==========QQ/Tim添加好友===========

        //==========微信添加好友==========
        buttonWeChat = findViewById(R.id.buttonWeChat);
        buttonWeChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hasClient(SupportActivity.this, WECHAT_PACKAGE_NAME)) {
                    clipboardManager.setPrimaryClip(ClipData.newPlainText("text", "HDG3334"));
                    showTipDialog(0, "微信账号已复制");
                    Intent intent = SupportActivity.this.getPackageManager().getLaunchIntentForPackage(WECHAT_PACKAGE_NAME);
                    startActivity(intent);
                } else {
                    showTipDialog(1, "微信未安装");
                }
            }
        });
        //==========微信添加好友==========

        //==========微信订阅号==========
        buttonWeChatMyHdg = findViewById(R.id.buttonWeChatMyHdg);
        buttonWeChatMyHdg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (hasClient(SupportActivity.this, WECHAT_PACKAGE_NAME)) {
                    clipboardManager.setPrimaryClip(ClipData.newPlainText("text", "MyHdg3334"));
                    showTipDialog(0, "订阅号账号已复制");
                    Intent intent = SupportActivity.this.getPackageManager().getLaunchIntentForPackage(WECHAT_PACKAGE_NAME);
                    startActivity(intent);
                } else {
                    showTipDialog(1, "微信未安装");
                }
            }
        });
        //==========微信订阅号==========

        //==========添加QQ群==========
        buttonQQGroup = findViewById(R.id.buttonQQGroup);
        buttonQQGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showTipDialog(1, "QQ群暂未创建");
//                if (!joinQQGroup("")) {
//                    showTipDialog(1, "QQ未安装或版本不支持");
//                }
            }
        });
        //==========添加QQ群==========

        //==========支付宝转账==========
        buttonAliPay = findViewById(R.id.buttonAlipay);
        buttonAliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hasClient(SupportActivity.this, ALIPAY_PACKAGE_NAME)) {
                    try {
                        Intent intent = Intent.parseUri(
                                INTENT_URL_FORMAT.replace("{urlCode}", "FKX06145VTGAIEUPLFFT4B"),
                                Intent.URI_INTENT_SCHEME
                        );
                        SupportActivity.this.startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    showTipDialog(1, "支付宝未安装");
                }
            }
        });
        //==========支付宝转账==========

        exitSupport = findViewById(R.id.exitSupport);//退出支持作者
        exitSupport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SupportActivity.this.finish();
            }
        });
    }

    private void showTipDialog(int mode, String text) {
        final QMUITipDialog tipDialog;
        switch (mode) {
            case 0://成功提示
                tipDialog = new QMUITipDialog.Builder(SupportActivity.this)
                        .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                        .setTipWord(text)
                        .create();
                break;
            case 1://失败提示
                tipDialog = new QMUITipDialog.Builder(SupportActivity.this)
                        .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                        .setTipWord(text)
                        .create();
                break;
            default://自定义提示
                tipDialog = new QMUITipDialog.Builder(SupportActivity.this)
                        .setTipWord(text)
                        .create();
                break;
        }
        tipDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        }, 900);
    }

    private boolean hasClient(Context context, String packageName) {//检测是否安装App
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            return info != null;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /****************
     * 发起添加群流程。群号：BIJOU内测() 的 key 为：
     * 调用 joinQQGroup() 即可发起手Q客户端申请加群 BIJOU内测()
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回false表示呼起失败
     ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri
                .parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }
}
